package com.vnet.bdz.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.naming.java.javaURLContextFactory;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.vnet.bdz.client.SiteService;

@SuppressWarnings("serial")
public class SiteServiceServlet extends RemoteServiceServlet implements
		SiteService {
	

	@Override
	public String getWeatherXML() {
			StringBuffer buffer = new StringBuffer();
        	try {            
        			URL url = new URL("http://www.google.com/ig/api?weather=Katy%20Wroclawskie&hl=pl");
        			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "ISO-8859-2" ));
        			String line;            
        			while ((line = reader.readLine()) != null) 
        			{
        				buffer.append(line);
        			}
       				reader.close();        
       			} catch (MalformedURLException e) 
  				{            
        						// ...        
				} catch (IOException e) 
				{            // ...           					
				}
				return buffer.toString();
	}

	@Override
	public String getBlogXML(String urlRSS) {
			StringBuffer buffer = new StringBuffer();
        	try {            
        			URL url = new URL(urlRSS);
        			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8" ));
        			String line;            
        			while ((line = reader.readLine()) != null) 
        			{
        				buffer.append(line);
        			}
       				reader.close();        
       			} catch (MalformedURLException e) 
  				{            
        						// ...        
				} catch (IOException e) 
				{            // ...           					
				}
				return buffer.toString();
	}

	
	@Override
	public String getPicasaAlbumsRSSXML(String rssURL) 
	{
        	try {            
        			StringBuffer buffer = new StringBuffer();
           			URL url = new URL(rssURL);
       				return transformXML(url.openStream());
       			} catch (MalformedURLException e) 
  				{            
       				System.out.print("MalformedURLException");
				} catch (IOException e) 
				{            
					System.out.print("IOException");
				}
			return null;
	}

	boolean isGroup = false;
	boolean readChars = false;
	private String transformXML(InputStream openStream) {
		SAXParserFactory factory = SAXParserFactory.newInstance();  
		factory.setValidating(false);
		
		try {
			SAXParser parser = factory.newSAXParser();
			output.delete(0, output.length());
			output.append("<?xml version='1.0' encoding='UTF-8'?>");
			parser.parse(openStream, new DefaultHandler(){
				@Override
				public void endDocument() throws SAXException {
					output.append("</root>");
					super.endDocument();
				}

				@Override
				public void startDocument() throws SAXException {
						output.append("<root>");
						super.startDocument();
				}

				@Override
				public void endElement(String uri, String name,
					    String qName)
						throws SAXException {
					if(qName.equals("item")) 
					{
						output.append("</item>");
						isGroup = false;
					}
					if(qName.equals("media:title") && isGroup)
					{
						output.append("</title>");
						readChars = false;
					}
					if(qName.equals("media:content") && isGroup)
					{
						output.append("</content>");
						readChars = false;
					}

					if(qName.equals("media:description") && isGroup)
					{
						output.append("</description>");
						readChars = false;
					}

					super.endElement(uri, name, qName);
				}

				@Override
				public void startElement(String uri, String name,
					    String qName, Attributes atts) throws SAXException {
					if(qName.equals("item"))
					{
						output.append("<item>");
						isGroup = true;
					}
					if(qName.equals("media:thumbnail") && isGroup)
					{
						output.append("<icon>");
						output.append(atts.getValue("url"));
						output.append("</icon>");
					}
					if(qName.equals("media:title") && isGroup)
					{
						output.append("<title>");
						readChars = true;
					}
					if(qName.equals("media:description") && isGroup)
					{
						output.append("<description>");
						readChars = true;
					}
					if(qName.equals("media:content") && isGroup)
					{
						output.append("<content>");
						readChars = true;
					}

					super.startElement(uri, name, qName, atts);
				}

				@Override
				public void characters(char[] arg0, int arg1, int arg2)
						throws SAXException {
					if(readChars) 
						output.append(arg0,arg1,arg2);
					super.characters(arg0, arg1, arg2);
				}});
				return output.toString();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
		
	}

	StringBuffer output = new StringBuffer();
	/*private String transformXML(StringBuffer buffer) 
	{
		SAXParserFactory factory = SAXParserFactory.newInstance();  
		factory.setValidating(false);
		
		try {
			SAXParser parser = factory.newSAXParser();
			output.delete(0, output.length());
			parser.parse(new StringReader(buffer.toString()), new DefaultHandler(){
				@Override
				public void endElement(String uri, String name,
					    String qName)
						throws SAXException {
					if(name.equals("item")) output.append("</item>");	
					super.endElement(uri, name, qName);
				}

				@Override
				public void startElement(String uri, String name,
					    String qName, Attributes atts) throws SAXException {
					
					if(name.equals("item")) output.append("<item>");
					super.startElement(uri, name, qName, atts);
				}});
			System.out.println(output.toString());
			return output.toString();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
		
	}*/

	
}

